/**
 * 
 */
package huajiu.com.interceptor;

import huajiu.com.base.ApiResponse;
import huajiu.com.service.UserService;
import huajiu.com.vo.ApiResponseVo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;














import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.sun.javafx.collections.MappingChange.Map;

/**
 * @author ItBotPjq
 *2018年2月11日
 */
public class ReponseInterceptor implements HandlerInterceptor {
	
	
	@Autowired
	UserService userService;

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		JSONObject jsonObject = getJsonString(request);
		System.out.println(getJsonString(request));
		System.out.println("成功");
	    PrintWriter outWriter =	response.getWriter();
	    outWriter.write(jsonObject.toString());
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}
	
	public JSONObject getJsonString(HttpServletRequest request){
		
		HashedMap resultMap = new HashedMap();
		ApiResponseVo responseVo = new ApiResponseVo();
		
		StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine()))
                data.append(line);
        } catch (IOException e) {
        } finally {
        }
        JSONObject jsonObject = JSONObject.parseObject(data.toString());
        if("".equals(jsonObject.get("mid").toString())){
        	responseVo.setReturnCode(responseVo.MID_NULL);
        	responseVo.setCode(1);
        	responseVo.setMessage("商户ID为空");
        	responseVo.setData("");
        	resultMap.put("response", responseVo);
        	resultMap.put("flag", false);
        }else if (!userService.checkMid(jsonObject.get("mid").toString())) {//不存在执行方法体
        	responseVo.setReturnCode(responseVo.WITHOUT_PERMISSION);
        	responseVo.setCode(1);
        	responseVo.setData("");
        	resultMap.put("response", responseVo);
        	resultMap.put("flag", false);		
        }else if ("".equals(jsonObject.get("sign").toString())) {
        	responseVo.setReturnCode(responseVo.SIGN_NULL);
        	responseVo.setCode(1);
        	responseVo.setMessage("sign为空");
        	responseVo.setData("");
        	resultMap.put("response", responseVo);
        	resultMap.put("flag", false);
		}
        
        
        return jsonObject;
	}

}
